package com.xinyue.game.center.business.account;

import com.xinyue.game.center.business.account.logic.AccountManager;
import com.xinyue.game.center.business.account.param.AccountLoginRequest;
import com.xinyue.game.center.business.account.param.AccountLoginResponse;
import com.xinyue.game.center.common.GameCenterConfig;
import com.xinyue.game.center.common.GameTokenService;
import com.xinyue.game.center.common.ServerErrorCode;
import com.xinyue.game.center.dao.AccountDao;
import com.xinyue.game.center.dao.entity.AccountEntity;
import com.xinyue.game.jwt.GameUserToken;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author 王广帅
 * @Date 2021/5/23 21:09
 */
@Service
public class AccountService {

    private static Logger logger = LoggerFactory.getLogger(AccountService.class);
    @Autowired
    private AccountManager accountManager;
    @Autowired
    private AccountDao accountDao;
    @Autowired
    private GameCenterConfig gameCenterConfig;
    @Autowired
    private GameTokenService gameTokenService;

    public AccountEntity login(AccountLoginRequest loginParam) {
        accountManager.checkUsername(loginParam.getUsername());
        accountManager.checkPassword(loginParam.getPassword());
        AccountEntity account = accountDao.findByUsername(loginParam.getUsername());
        checkAccountExist(account, loginParam);
        return account;
    }


    public AccountLoginResponse getLoginResponse(AccountEntity account) throws Exception {
        AccountLoginResponse loginResponse = new AccountLoginResponse();
        loginResponse.setUserId(account.getId());
        loginResponse.setUsername(account.getUsername());
        loginResponse.setRegisterTime(account.getRegisterTime());
        if (account.getSectionPlayerMap() != null) {
            account.getSectionPlayerMap().forEach((k, v) -> {
                String token = gameTokenService.createPlayerToken(account.getId(), v.getPlayerId(), k);
                v.setToken(token);
                logger.debug("角色创建token成功，playerId: {},token: {}", v.getPlayerId(), token);
            });
        }
        loginResponse.setSectionPlayerMap(account.getSectionPlayerMap());
        GameUserToken gameUserToken = new GameUserToken();
        gameUserToken.setUserId(account.getId());
        String token = gameTokenService.createAccountToken(gameUserToken);
        loginResponse.setToken(token);
        return loginResponse;
    }

    private void checkAccountExist(AccountEntity account, AccountLoginRequest loginParam) {
        if (account == null) {
            throw ServerErrorCode.ACCOUNT_1006.exception();
        }
        if (!account.getPassword().equals(loginParam.getPassword())) {
            throw ServerErrorCode.ACCOUNT_10087.exception();
        }
    }

    public AccountEntity getAndCheckAccount(String userId) {
        AccountEntity accountEntity = accountDao.findById(userId).orElseThrow(() -> ServerErrorCode.ACCOUNT_1006.exception());
        return accountEntity;
    }

    public void updateAccount(AccountEntity accountEntity) {
        accountDao.save(accountEntity);
    }
}
